<?php

namespace common\rest\models\c2\statics;

use Yii;

/**
 * Description of ResponseDatum
 *    data format type
 *
 * @author benb
 */
class ResponseDatum
{

    public static function getSuccessMeta($data = null, $meta = [], $html = false)
    {
        $default = [
            'code' => OperationResult::SUCCESS,
            'msg' => Yii::t('app.c2', 'Operation success.'),
            'time' => date('Y-m-d H:i:s'),
            'extraData' => null,
        ];

        if (isset($meta['msg']) && is_array($meta['msg'])) {
            $meta['msg'] = self::formatMsg($meta['msg']);
        }

        if ($html) {
            $meta['msg'] = '<div class=\'alert-box done\'><i class=\'fa-check-circle\'></i> ' . $meta['msg'] . '</div>';
        }

        return array_merge($default, $meta);
    }

    public static function getErrorMeta($data = null, $meta = [], $html = false)
    {
        $default = [
            'code' => OperationResult::ERROR,
            'msg' => Yii::t('app.c2', 'Operation fail.'),
            'time' => date('Y-m-d H:i:s'),
            'extraData' => null,
        ];

        if (isset($meta['msg']) && is_array($meta['msg'])) {
            $meta['msg'] = self::formatMsg($meta['msg']);
        }

        if ($html) {
            $meta['msg'] = '<div class=\'alert-box done\'><i class=\'fa-check-circle\'></i> ' . $meta['msg'] . '</div>';
        }

        return array_merge($default, $meta);
    }

    /**
     * standardize the datum format
     * @param array $meta
     * @param array $data - return data fields
     * @param bool $html
     * @param bool $isLog
     * @return array ['data'=>[], 'meta'=[]]
     */
    public static function getSuccessDatum($data = null, $meta = [], $html = false, $isLog = false)
    {
        return [
            'data' => $data, 'meta' => self::getSuccessMeta($data, $meta, $html),
        ];
    }

    /**
     * standardize the datum format
     * @param array $data - return data fields
     * @param array $meta
     * @param bool $html
     * @param bool $isLog
     * @return array
     */
    public static function getErrorDatum($data = null, $meta = [], $html = false, $isLog = false)
    {
        return [
            'data' => $data, 'meta' => self::getErrorMeta($data, $meta, $html),
        ];
    }

    public static function formatMsg($msgs)
    {
        $str = "";
        foreach ($msgs as $msg) {
            $str .= "- " . $msg . "<br/>\n";
        }
        return $str;
    }

}
